package com.zhoug.http3;

import androidx.annotation.NonNull;

/**
 * 请求url创建工具
 *
 * @Author 35574
 * @Date 2021/9/10
 * @Description
 */
public class RequestUrlBuild {
    private String mUrl;
    private String mParams;

    private RequestUrlBuild(@NonNull String url) {
        url = url.trim();
        int index = url.indexOf("?");
        if (index >= 0) {
            //url中已经包含参数?
            mUrl = url.substring(0, index);//去掉参数后的url
            mParams = url.substring(index);//参数
            if (mParams.length() > 1 && !mParams.endsWith("&")) {
                mParams += "&";
            }
        } else {
            //url中不包含参数?
            if (!url.endsWith("/")) {
                mUrl = url + "/";
            } else {
                mUrl = url;
            }
            mParams = "?";
        }
    }

    public static RequestUrlBuild with(String url) {
        return new RequestUrlBuild(url);
    }

    /**
     * 设置参数
     *
     * @param key
     * @param value
     * @return
     */
    public RequestUrlBuild putParams(String key, Object value) {
        if (key == null) {
            return this;
        } else if (value == null) {
            mParams += key + "=" + "&";
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, String value) {
        if (key == null) {
            return this;
        } else if (value == null) {
            mParams += key + "=" + "&";
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, int value) {
        if (key == null) {
            return this;
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, long value) {
        if (key == null) {
            return this;
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, short value) {
        if (key == null) {
            return this;
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, float value) {
        if (key == null) {
            return this;
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, double value) {
        if (key == null) {
            return this;
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }

    public RequestUrlBuild putParams(String key, boolean value) {
        if (key == null) {
            return this;
        } else {
            mParams += key + "=" + value + "&";
        }
        return this;
    }





    public String build() {
        if (mParams.endsWith("&")) {
            mParams = mParams.substring(0, mParams.length() - 1);
        }
        return mUrl + mParams;
    }


}
